package tech.kpretty.pool;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Demo {
    public static void main(String[] args) {
        // 固定数量的线程池
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
        // 单个线程池
        ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
        // 可扩容线程池
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        //
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(10);
        try {
            for (int i = 1; i <= 10; i++) {
                int finalI = i;
                fixedThreadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "处理任务：" + finalI);
                });
            }
        } finally {
            // 关闭线程池
            fixedThreadPool.shutdown();
        }

    }
}
